﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Ewts.ashx
{
    /// <summary>
    /// Summary description for Tally
    /// </summary>
    public class Tally : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string strjson = "";
            DataLib.Model.Tally tally = new DataLib.Model.Tally();
   

            //处理前删除操作
            if (context.Request["afterid"] != null && context.Request["afterid"].ToString().Trim() != "")
            {
                new DataLib.BLL.TallyAfter().Delete(int.Parse(context.Request["afterid"].ToString().Trim()));
            }
            //处理后删除操作
            else if (context.Request["beforeid"] != null && context.Request["beforeid"].ToString().Trim() != "")
            {
                new DataLib.BLL.TallyBefore().Delete(int.Parse(context.Request["beforeid"].ToString().Trim()));
            }

            else if (context.Request["tallyid"] != null && context.Request["tallyid"].ToString().Trim() != "" && context.Request["tallyid"].ToString().Trim() != "0")
            {
                tally = new DataLib.BLL.Tally().GetModel(int.Parse(context.Request["tallyid"].ToString().Trim()));
            }
            else
            {
                tally = AddTally(context);
            }
           
            //处理前
            if (context.Request["isab"] != null && context.Request["isab"].ToString().Trim() == "b")
            {
                DataLib.Model.TallyBefore bmodel = AddBefore(context, (int)tally.id);
                strjson += serializer.Serialize(bmodel);
                strjson += ",";
            }
            else
            {
                DataLib.Model.TallyAfter amodel = AddAfter(context, (int)tally.id);
                strjson += serializer.Serialize(amodel);
                strjson += ",";
            }
            strjson += serializer.Serialize(tally);
            strjson = "[" + strjson + "]";
            context.Response.Write(strjson);
        }

        

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 原料理货处理前数据添加或者更新
        /// </summary>
        /// <param name="context"></param>
        protected DataLib.Model.TallyBefore AddBefore(HttpContext context,int tallyid)
        {
            DataLib.Model.Tally mTally = new DataLib.BLL.Tally().GetModel(tallyid);
            DataLib.Model.TallyBefore bfmodel = new DataLib.Model.TallyBefore();
            bfmodel.classify = getURLstr(context.Request["con"]);
            bfmodel.goodscode = getURLstr(context.Request["gcode"]);
            bfmodel.goodsName = getURLstr(context.Request["gname"]);
            bfmodel.netwegight = getURLdecimal(context.Request["nt"]);
            bfmodel.quantity = getURLint(context.Request["qty"]);
            bfmodel.remark = getURLstr(context.Request["remark"]);
            bfmodel.rubbishcode = getURLstr(context.Request["rubc"]);
            bfmodel.rubbishname = getURLstr(context.Request["rubt"]);
            bfmodel.boxcount = getURLint(context.Request["boxcount"]);
            bfmodel.remark = getURLstr(context.Request["remark"]);
            bfmodel.kupono = getURLstr(context.Request["ldh"]);
            bfmodel.boxno = getURLstr(context.Request["boxno"]);
            bfmodel.id = getURLint(context.Request["befid"]);
           
            bfmodel.status = -1;
            bfmodel.tallyid = tallyid;
            bfmodel.tallyno = mTally.tallyno;
            bfmodel.loadid = mTally.loadid;
            bfmodel.loadnum = mTally.LoadNo;
            if (context.Request["befid"] != null && context.Request["befid"].ToString().Trim() != "0")
            {
                bfmodel.id = int.Parse(context.Request["befid"].ToString().Trim());
                new DataLib.BLL.TallyBefore().Update(bfmodel);
            }
            else
            {
                bfmodel.id = new DataLib.BLL.TallyBefore().Add(bfmodel);
            }
            return bfmodel;
        }

        /// <summary>
        /// 原料理货处理后数据添加或者更新
        /// </summary>
        /// <param name="context"></param>
        protected DataLib.Model.TallyAfter AddAfter(HttpContext context, int tallyid)
        {
            DataLib.Model.Tally mTally = new DataLib.BLL.Tally().GetModel(tallyid);
            DataLib.Model.TallyAfter afmodel = new DataLib.Model.TallyAfter();
            afmodel.classify = getURLstr(context.Request["con"]);
            afmodel.goodscode = getURLstr(context.Request["gcode"]);
            afmodel.goosdname = getURLstr(context.Request["gname"]);
            afmodel.nt = getURLdecimal(context.Request["nt"]);
            afmodel.qty = getURLint(context.Request["qty"]);
            afmodel.remark = getURLstr(context.Request["remark"]);
            afmodel.rubbishcode = getURLstr(context.Request["rubc"]);
            afmodel.rubbishname = getURLstr(context.Request["rubt"]);
            afmodel.boxcount = getURLint(context.Request["boxcount"]);
            afmodel.remark = getURLstr(context.Request["remark"]);
            afmodel.kupono = getURLstr(context.Request["ldh"]);
            afmodel.boxno = getURLstr(context.Request["boxno"]);
            afmodel.status = -1;

            afmodel.Tallyid = tallyid;
            afmodel.loadid = mTally.loadid;
            afmodel.loadnum = mTally.LoadNo;
            afmodel.tallyno = mTally.tallyno;
            if (context.Request["aftid"] != null && context.Request["aftid"].ToString().Trim() != "0")
            {
                afmodel.id = int.Parse(context.Request["aftid"].ToString().Trim());
                new DataLib.BLL.TallyAfter().Update(afmodel);
            }
            else
            {
                afmodel.id = new DataLib.BLL.TallyAfter().Add(afmodel);
            }

            return afmodel;
        }

        /// <summary>
        /// 添加原料理货记录
        /// </summary>
        /// <param name="contex"></param>
        /// <returns></returns>
        protected DataLib.Model.Tally AddTally(HttpContext contex)
        {
            DataLib.Model.Tally tmodel = new DataLib.Model.Tally();
            DataLib.BLL.Tally tbll = new DataLib.BLL.Tally();

            int delid;
            tmodel.CJONO = getURLstr(contex.Request["cjo"]);
            tmodel.InWrlNo = getURLstr(contex.Request["wrlno"]);
            tmodel.LoadNo = getURLstr(contex.Request["loadnum"]);
            tmodel.OperDate = DateTime.Now;
            tmodel.loadid = getURLint(contex.Request["loadid"]);
            tmodel.outid = getURLint(contex.Request["outid"]);
            tmodel.inDate = DateTime.Now;
            tmodel.status = -1;
            tmodel.tallyno = tbll.GetMaxTallyNo(out delid);
            tmodel.id = tbll.Add(tmodel);
            HttpCookie cookie = new HttpCookie("tally");
            cookie.Values.Add("tallyid", tmodel.id.ToString());
            //contex.Response.AppendCookie(cookie);
            if (delid != 0)
            {
                DataLib.BLL.tab_delNo bdelno = new DataLib.BLL.tab_delNo();
                DataLib.Model.tab_delNo mdelno = bdelno.GetModel(delid);
                mdelno.isdel = -1;
                bdelno.Update(mdelno);
            }
            return tmodel;
        }

        /// <summary>
        /// 格式化URL 字符串zxsa
        /// </summary>
        /// <returns></returns>
        public string getURLstr(object strurlpar)
        {
            string value = "";
            if (strurlpar != null && strurlpar.ToString().Trim() != "")
                value = strurlpar.ToString().Trim();
            return value;
        }

        /// <summary>
        /// 格式化URL 整型
        /// </summary>
        /// <param name="strurlpar"></param>
        /// <returns></returns>
        public int getURLint(object strurlpar)
        {
            int value = 0;
            if (strurlpar != null && strurlpar.ToString().Trim() != "")
            {
                try
                {
                    value = int.Parse(strurlpar.ToString().Trim());
                }
                catch
                { }
            }
            return value;
        }

        /// <summary>
        /// 格式化URL decimal型
        /// </summary>
        /// <param name="strurlpar"></param>
        /// <returns></returns>
        public decimal getURLdecimal(object strurlpar)
        {
            decimal value = 0;
            if (strurlpar != null && strurlpar.ToString().Trim() != "")
            {
                try
                {
                    value = decimal.Parse(strurlpar.ToString().Trim());
                }
                catch
                { }
            }
            return value;
        }

    }
}